<!-- 订单管理 -->
<template>
  <div class="dashboard-container order-list customer-list-box">
    <!-- 头部卡片部分 -->
    <el-card>
      <el-card class="TopCard">
        <el-form
          ref="form"
          label-width="90px"
          :model="fromData"
        >
          <!-- 第一行 -->
          <el-row :gutter="60">
            <!-- gutter 栅格间隔 控制边距 -->
            <el-col :span="8">
              <!-- span 默认是24份一行 平分 -->
              <el-form-item
                label="订单编号:"
                prop="id"
              >
                <el-input
                  v-model="fromData.id"
                  placeholder="请输入订单编号"
                  type="text"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <!-- 第一行第二个 -->
            <el-col :span="8">
              <el-form-item
                label="订单状态:"
                prop="status"
              >
                <el-select
                  v-model="fromData.status"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in orderstatus"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 第一行第三个 -->
            <el-col :span="8">
              <el-form-item
                label="付费状态:"
                prop="paymentStatus"
              >
                <el-select
                  v-model="fromData.paymentStatus"
                  collapse-tags
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in options2"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第二行 -->
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="发件人姓名:"
                prop="senderName"
              >
                <el-input
                  v-model="fromData.senderName"
                  placeholder="请输入发件人姓名"
                  type="text"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <!-- 第二行第二个 -->
            <el-col :span="8">
              <el-form-item
                label="发件人电话:"
                prop="senderPhone"
              >
                <el-input
                  v-model="fromData.senderPhone"
                  placeholder="请输入发件人电话"
                  type="text"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <!-- 第二行第三个 -->
            <el-col :span="8">
              <el-form-item
                class="page-form-item"
                label="发件人地址:"
              >
                <el-select
                  v-model="fromData.senderProvinceId"
                  placeholder="请选择省"
                  style="width: 85px"
                  value-key="id"
                  clearable
                  @change="getSenderProvince"
                >
                  <el-option
                    v-for="item in provinceList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>

                <el-select
                  v-model="fromData.senderCityId"
                  placeholder="请选择市"
                  clearable
                  style="width: 85px"
                  @change="getSenderCity"
                >
                  <el-option
                    v-for="item in cityList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
                <el-select
                  v-model="fromData.senderCountyId"
                  value-key="id"
                  clearable
                  style="width: 85px"
                  placeholder="请选择县/区"
                >
                  <el-option
                    v-for="item in countryList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第三行 -->
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="收件人姓名:"
                prop="receiverName"
              >
                <el-input
                  v-model="fromData.receiverName"
                  placeholder="请输入收件人姓名"
                  type="text"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <!-- 第三行第二个 -->
            <el-col :span="8">
              <el-form-item
                label="收件人电话:"
                prop="receiverPhone"
              >
                <el-input
                  v-model="fromData.receiverPhone"
                  placeholder="请输入收件人电话"
                  type="text"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <!-- 第三行第三个 -->
            <el-col :span="8">
              <el-form-item
                class="page-form-item"
                label="收件人地址:"
              >
                <el-select
                  v-model="fromData.receiverProvinceId"
                  placeholder="请选择省"
                  value-key="id"
                  clearable
                  style="width: 85px"
                  @change="getProvinceReceiver"
                >
                  <el-option
                    v-for="item in provinceList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>

                <el-select
                  v-model="fromData.receiverCityId"
                  placeholder="请选择市"
                  clearable
                  style="width: 85px"
                  @change="getReceiverCity"
                >
                  <el-option
                    v-for="item in receiverCityList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
                <el-select
                  v-model="fromData.receiverCountyId"
                  value-key="id"
                  clearable
                  placeholder="请选择县/区"
                  style="width: 85px"
                >
                  <el-option
                    v-for="item in countryReceiverList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第四行 -->
          <el-row>
            <el-row>
              <el-button
                type="warning"
                @click="handleFilter"
              >搜索</el-button>
              <el-button @click="resetForm('form')">重置</el-button>
            </el-row>
          </el-row>
        </el-form>
      </el-card>
      <!-- 渲染部分 -->
      <el-card
        v-loading="loading"
        class="BottomCard"
        style="margin-top: 20px"
      >
        <!-- 表格头部 -->
        <el-table
          :data="list"
          style="width: 100%"
        >
          <el-table-column
            label="序号"
            width="90"
            type="index"
            :index="xuhaopaixu"
          >
          </el-table-column>
          <el-table-column
            prop="id"
            label="订单编号"
            width="160"
          >
          </el-table-column>
          <el-table-column
            prop="transportOrder.id"
            label="运单编号"
            width="160"
          >
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="下单时间"
            width="160"
          >
          </el-table-column>
          <el-table-column
            prop="status"
            label="订单状态"
            width="120"
          >
            <template #default="{ row }">
              {{ genghuan(row.status) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="senderName"
            label="发件人姓名"
            width="120"
          >
          </el-table-column>
          <el-table-column
            prop="senderPhone"
            label="发件人电话"
            width="120"
          >
          </el-table-column>
          <el-table-column
            prop="senderAddress"
            label="发件人地址"
            width="160"
          >
          </el-table-column>
          <el-table-column
            prop="receiverName"
            label="收件人姓名"
            width="120"
          >
          </el-table-column>
          <el-table-column
            prop="receiverPhone"
            label="收件人电话"
            width="120"
          >
          </el-table-column>
          <el-table-column
            prop="receiverAddress"
            label="收件人地址"
            width="160"
          >
          </el-table-column>
          <el-table-column
            prop="pickupType"
            label="取件类型"
            width="120"
          >
            <template #default="{ row }">
              {{ row.pickupType === 1 ? '网点自寄' : '上门取件' }}
            </template>
          </el-table-column>
          <el-table-column
            prop="paymentMethod"
            label="付费类型"
            width="120"
          >
            <template #default="{ row }">
              {{ row.paymentMethod === 1 ? '预结' : '到付' }}
            </template>
          </el-table-column>
          <el-table-column
            prop="paymentStatus"
            label="付费状态"
            width="120"
          >
            <template #default="{ row }">
              {{ row.paymentStatus === 1 ? '未付' : '已付款' }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="120"
            fixed="right"
          >
            <template #default="{ row }">
              <el-button
                type="text"
                @click="$router.push(`/business/edit-order?id=${row.id}`)"
              >查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 表格尾部分页  -->
        <div
          class="block"
          style="height: 60px; margin-top: 10px;margin-left: 220px;"
        >
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="+counts"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </div>
      </el-card>
    </el-card>
  </div>
</template>
<script>
import { areaList } from '@/api/common'
import { orderList } from '@/api/order'
import orderstatus from '@/api/orderstatus'
export default {
  name: 'OrderList',
  data() {
    return {
      num: {},
      countryList: [],
      countryReceiverList: [],
      receiverCityList: [],
      provinceList: [],
      cityList: [],
      // countryList: [],
      // 头部卡片部分
      orderstatus: orderstatus,
      options1: [],
      options2: [
        {
          value: '1',
          label: '未付'
        },
        {
          value: '2',
          label: '已付'
        }
      ],

      // 底部卡片
      tableData: [],

      // 分页部分
      currentPage: 1,
      //
      fromData: {
        id: '',
        status: '',
        paymentStatus: '',
        senderPhone: '',
        receiverName: '',
        receiverPhone: '',
        senderName: '',
        senderProvinceId: '',
        senderCountyId: '',
        senderCityId: '',
        receiverProvinceId: '',
        receiverCityId: '',
        receiverCountyId: ''
      },
      // 列表渲染
      list: [],
      orderList: [],
      //
      page: 1,
      pageSize: 10,
      counts: 0,
      loading: false
    }
  },
  created() {
    document.onkeydown = function (e) {
      var key = window.event.keyCode
      if (key === 13) {
        this.handleFilter(this.form)
      }
    }
    // 列表渲染做
    this.getOrderList()
    // 地区数据初始化
    this.showcity()
  },
  methods: {
    // 获取发件人地址
    async getSenderProvince(vid) {
      if (this.fromData.senderProvinceIdList !== 0) {
        this.num.parentId = this.fromData.senderProvinceIdList
        const { data } = await areaList(this.num)
        this.cityList = data
      }
    },
    // 获取收件人地址
    async getProvinceReceiver() {
      if (this.fromData.receiverProvinceId !== 0) {
        this.num.parentId = this.fromData.receiverProvinceId
        const { data } = await areaList(this.num)
        this.receiverCityList = data
      }
    },
    // 获取省
    async showcity() {
      this.num.parentId = 0
      const { data } = await areaList(this.num)
      this.provinceList = data
    },
    /**
     * 市级数据id
     */
    getSenderCity(vId) {
      this.fromData.senderCountyId = ''
      if (vId !== '') {
        let obj = {}
        obj = this.cityList.find((item) => {
          return item.id === vId
        })
        this.getCity()
        this.fromData.senderCityId = obj.id
      }
    },
    getReceiverCity(vId) {
      this.fromData.receiverCountyId = ''
      if (vId !== '') {
        let obj = {}
        obj = this.receiverCityList.find((item) => {
          return item.id === this.fromData.receiverCityId
        })
        this.receiverCity()
        this.fromData.receiverCityId = obj.id
      }
    },
    // 区县
    async receiverCity() {
      if (this.fromData.receiverProvinceId !== 0) {
        this.num.parentId = this.fromData.receiverCityId
        const { data } = await areaList(this.num)
        this.countryReceiverList = data
        this.fromData.receiverCountyId = ''
      }
    },
    async getCity() {
      if (this.fromData.senderProvinceId !== 0) {
        this.num.parentId = this.fromData.senderCityId
        const { data } = await areaList(this.num)
        this.countryList = data
        this.fromData.senderCountyId = ''
      }
    },
    // // 三级联动之县区联动
    // CityListAreaS() {
    //   this.value7S = this.value6S.filter(item => item.areaId === this.listvalue7S)[0].counties
    //   // console.log(this.value7)
    //   this.listvalue8S = ''
    // },
    // // 城市三级联动处理之市联动
    // CityListS() {
    //   // console.log(this.listvalue6)
    //   this.value6S = this.city.filter(item => item.areaId === this.listvalue6S)
    //   this.value6S = this.value6S[0].cities
    //   // console.log(this.value6)
    //   this.listvalue7S = ''
    //   this.listvalue8S = ''
    // },
    // // 三级联动之县区联动
    // CityListArea() {
    //   this.fromData.senderCountyId = this.fromData.senderCityId.filter(item => item.areaId === this.listvalue7)[0].counties
    //   // console.log(this.value7)
    //   this.listvalue8 = ''
    // },
    // // 城市三级联动处理之市联动
    // CityList() {
    //   // console.log(this.listvalue6)
    //   this.fromData.senderCityId = this.city.filter(item => item.areaId === this.listvalue6)
    //   this.value6 = this.value6[0].cities
    //   // console.log(this.value6)
    //   this.listvalue7 = ''
    //   this.listvalue8 = ''
    // },
    // 映射处理数据
    genghuan(id) {
      const att = orderstatus.filter((item) => {
        return item.id === id
      })
      // console.log(att[0])
      return att[0].name
    },

    // 重置表单
    resetForm() {
      this.fromData = {
        id: '',
        status: '',
        paymentStatus: '',
        senderPhone: '',
        receiverName: '',
        receiverPhone: '',
        senderName: '',
        senderProvinceId: '',
        senderCountyId: '',
        senderCityId: '',
        receiverProvinceId: '',
        receiverCityId: '',
        receiverCountyId: ''
      }
      this.getOrderList()
    },
    // 列表渲染做
    async getOrderList() {
      // const data = Object.assign(this.fromData, this.page, this.pageSize)
      // console.log(data);
      this.loading = true
      const res = await orderList({
        ...this.fromData,
        page: this.page,
        pageSize: this.pageSize
      })
      this.counts = res.data.counts
      this.list = res.data.items
      this.loading = false
    },
    // 搜索信息
    handleFilter() {
      this.getOrderList()
    },
    // 分页部分
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getOrderList()
    },
    // 分页部分
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getOrderList()
    },
    xuhaopaixu(index) {
      return (this.page - 1) * this.pageSize + index + 1
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}

// TopCard 添加样式
.el-col {
  border-radius: 4px;
}

//  BottomCard  添加样式
.el-table {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
</style>

<style lang="scss" scoped>
.order-list /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
//@at-root
// 搜索
// .TopCard {
//   margin: 0 15px;
// }
.page-form-item {
  font-size: 14px;
}
</style>
